<?php

namespace App\Services\Qr;

use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Endroid\QrCode\Label\Label;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;

class QrService
{
    protected $url;
    protected $type;

    public function init(string $url, string $type)
    {
        $this->url = $url;
        $this->type = $type;
        return $this;

    }

    public function make()
    {

        $writer = new PngWriter();

// 生成二维码
        $qrCode = QrCode::create($this->url)
            ->setEncoding(new Encoding('UTF-8'))
            ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
            ->setSize(300)
            ->setMargin(0)
            ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
            ->setForegroundColor(new Color(0, 0, 0))
            ->setBackgroundColor(new Color(255, 255, 255));

// 创建通用标识
        $logo = Logo::create(public_path('qr') . '/qr.png')
            ->setResizeToWidth(50);

// 创建通用标签
        $label = Label::create($this->type)
            ->setTextColor(new Color(238, 39, 70));

        $result = $writer->write($qrCode, $logo, $label);
        return $result->getDataUri();

    }

}
